package com.example.hotel.controller;


import com.example.hotel.entity.Employee;
import com.example.hotel.service.impl.EmployeeServiceImpl;
import com.example.hotel.util.DESUtil;
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 员工表 前端控制器
 * </p>
 *
 * @author lu
 * @since 2019-12-29
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Resource
    @Getter
    @Setter
    private EmployeeServiceImpl employeeService;

    /**
     * 遍历出员工
     * @param ename
     * @param map
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/selectAllEmployee")
    @ResponseBody
    public Map selectAllEmployee(String ename,Map map,int pageNum, int pageSize){
        map.put("pageSize", pageSize);
        map.put("ename", ename);
        int a = pageSize * (pageNum - 1);
        map.put("a", a);
        List<Employee> employeeList = employeeService.selectAll(map);

        for (Employee employee: employeeList ) {    //解密身份证和手机号
            String jiemimPhone=DESUtil.decryptBasedDes(employee.getEphone());
            String jiemiIdCard =DESUtil.decryptBasedDes(employee.getEidcard());
            employee.setEphone(jiemimPhone);
            employee.setEidcard(jiemiIdCard);
        }

        int count = employeeService.selectAllEmployeeByEnameCount(map);
        int b = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;                              //count/pageSize求多少页
        Map map1 = new HashMap();
        map1.put("employee", employeeList);
        map1.put("pageNum", pageNum);
        map1.put("count",count);
        map1.put("ct", b);
        return map1;
    }

    /**
     * 修改页面加载就执行的根据id查
     * @param
     * @param
     * @return
     */
    @RequestMapping("/editEmployee")
    @ResponseBody
    public List editEmployee(Employee employee,HttpSession session){
        List ls=new ArrayList();
        String phone= DESUtil.encryptBasedDes(employee.getEphone());
        employee.setEphone(phone);
        employeeService.updateEmployeeById(employee);
        session.removeAttribute("employee");    //销毁session
        return ls;
   }

    /**
     * 去修改页面
     * @return
     */
    @RequestMapping("/toEdit/{eid}")
    public ModelAndView toEdit(@PathVariable("eid") int eid, Map map, HttpSession session){
        Employee employee=employeeService.selectAllById(eid);
        String phone=DESUtil.decryptBasedDes(employee.getEphone());
        employee.setEphone(phone);
        String idcard=DESUtil.decryptBasedDes(employee.getEidcard());
        employee.setEidcard(idcard);
        session.setAttribute("employee",employee);
        return new ModelAndView("H-ui.admin/employee_edit_list.html");
    }

    /**
     * 根据ID删除
     * @param eid
     * @return
     */
    @RequestMapping("/delAllBId")
    public List delAllBId(int eid){
        employeeService.delAllBId(eid);
        List ls=new ArrayList();
        return ls;
    }

    /**
     * 去添加页面
     * @return
     */
    @RequestMapping("/toAdd")
    public ModelAndView toAdd(){
        return new ModelAndView("H-ui.admin/employee-add.html");
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @RequestMapping("/addEmployee")
    public Map addAmployee(Employee employee){
        employee.setEphone(DESUtil.encryptBasedDes(employee.getEphone()));
        employee.setEidcard(DESUtil.encryptBasedDes(employee.getEidcard()));
        employeeService.addEmployee(employee);
        Map m =new HashMap();
        return m;
    }

}

